


var flag = parseInt("" == "" ? 1 : "");
var savetype = "";  //  1.autodraft 2.persondraft 3.personsubmit
var g_saveDraftcount = 0;
var g_saveDraft_done = false;
var g_saveDraft_content = '';

function autoSave(){
	var obj = document.getElementById("autosavebutton");
	saveDiary(obj, 'adddraft');
}

function clearmsg(){
	document.getElementById('msg').innerHTML='';
}
function saveDiary(v_btn, v_prompt)
{
	savetype = v_prompt=="adddraft"?"adddraft":"personsubmit";
	document.writeform.target = "";
	document.writeform.draft.value = "0";

	if (writeSubmit(document.writeform))
	{
		if (v_btn) v_btn.disabled = true;
		if ($(v_prompt)) s(v_prompt);
		
		document.writeform.submit();
	}
}

function writeSubmit(v_form){
	v_form.content.value = content_editor.getHtml();
	v_form.title.value = v_form.title.value;
	var st = document.getElementById('save_type');

	if(savetype=='adddraft')
		st.value = 0;
	else
		st.value = 1;
	var text_length = textLength();
	if(savetype!="adddraft"){
		if (v_form.title.value.length > 50 || v_form.title.value.length <= 0){
				//alert("标题长度为1~50个字");
				new MessageBox(MessageBox.TYPE_MESSAGE,'标题长度为1~50个字', '', {});
				v_form.title.focus();
				return false;
		}
				
		if(text_length < 1&&v_form.content.value<1){
			//alert('正文内容不能为空');
			new MessageBox(MessageBox.TYPE_MESSAGE,'您的日记没有正文，请写点儿什么吧', '', {});
			content_editor.getFocus();
			return false;
		}

		return true;
	}else{
		if(text_length < 1&&v_form.content.value<1)	return false;
		d = new Date();
		var title = document.getElementById('title').value;
		var category =  document.getElementById('category').value;
		var srank =  document.getElementById('srank').value;

		new Ajax.Request('/?_app=blog&_controller=index&_action=send_form', {
						method: 'post',
						parameters: $H({thread_id:v_form.thread_id.value,title:title,content:v_form.content.value,category:v_form.category.value,srank:srank,save_type:0}).toQueryString(),
						onComplete: function(transport){
							if(transport.responseText>0){
									document.getElementById('thread_id').value = transport.responseText;
									$('container').insert("<span class=\"friend-tip\" id='prompt_msg'>草稿已保存</span>");
									new Deletion('prompt_msg', 3000);
							}
								
						}
		});
		g_saveDraft_title = v_form.title.value;
		g_saveDraft_content = v_form.content.value;

		return false;
	}
}


function textLength(){
	var str = content_editor.getHtml();
	str = str.replace(/<\/?.+?>/g,"");
	str = str.replace(/\s+/g,"");
	return str.length;
}


function SetCookie(cookieName, cookieValue, nHours)
{
    var today = new Date();
    var expire = new Date();
    if (nHours == null || nHours == 0)
     nHours = 1;
    expire.setTime(today.getTime() + 1000 * 60 * nHours);
    document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" +
     expire.toGMTString();
}

function GetCookie(cookieName)
{
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == - 1 || cookieName == "")
     return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == - 1)
     ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}

function _bodyonbeforeunload(event)
{
	event = $E(event);
	if(document.writeform.title.value.length == 0 && content_editor.getHtml().length==0)
	{
		return;
	}
	if(savetype == "personsubmit")
	{
		return;
	}
	

	if (content_editor.getHtml() == g_saveDraft_content)
	{
		//alert(document.writeform.title.value.length)
		if (document.writeform.title.value.length)
		{
			if (g_saveDraft_title == document.writeform.title.value)
			{
				return;
			}
		}
		else
		{	
			if (g_saveDraft_title == document.writeform.title.value)
			{
				return;
			}
		}
	}

	event.returnValue = "╭──────────────────╮\nㄧ  你写的日记还没保存,确定要离开吗?  ㄧ\n╰──────────────────╯";

}

